///*
// * SalesBatchTester.java
// *
// * Created on March 29, 2007, 12:14 PM
// *
// * To change this template, choose Tools | Template Manager
// * and open the template in the editor.
// */
//
//package edu.byu.isys413.samlam;
//
//import java.sql.Connection;
//import java.sql.PreparedStatement;
//import java.sql.ResultSet;
//import java.sql.SQLException;
//import java.util.LinkedList;
//import java.util.List;
//
///**
// * 
// * sales batch
// * @author Ashton Eggett
// */
//public class SalesBatch {
//
//    
//    /** Creates a new instance of SalesBatchTester */
//    public SalesBatch() {
//    }
//    
//    /**
//     * main
//     * @param args the command line arguments
//     * @throws edu.byu.isys413.samlam.DataException exception
//     * @throws java.sql.SQLException exception
//     * @throws java.lang.Exception exception
//     */
//    public static void main(String[] args) throws DataException, SQLException, Exception {
//        // TODO code application logic here
//        StoreProduct storeprod = StoreProductDAO.getInstance().create();
//        List<StoreProduct> list = new LinkedList();
//        
//        
//        list = StoreProductDAO.getInstance().getAlltoOrder();
//        
//        //loop through vendor items where conceptualid = the storeproduct's conceptualid, find the
//        //one with the lowest cost
//        
//        //this for loop will go through the rs and create a string of all the conceptualID's that need to
//        //ordered. if we're on the last of the list, we don't include the extra spacing or the words or
//        String toOrderId = "";
//        for(int i = 0; i<list.size(); i++) {
//            if(i == list.size()){
//                toOrderID += list.get(i).getConceptual().getCpguid();
//            }else {
//                toOrderID += list.get(i).getConceptual().getCpguid() + ", ir";
//            }//end else
//        }//end for
//        
//        Connection conn = ConnectionPool.getInstance().get();
//        PreparedStatement query = conn.prepareStatement("Select min(cost), vendorID, conceptualID from VendorItem where conceptualID = ? order by VendorID");
//        query.setString(1, toOrderId);
//        ResultSet rs = query.executeQuery();
//        
//        
//        String globalVendor = null;
//        
//        while(rs.next()) {
//            PurchaseOrder po = null;
//            List<OrderLine> oList = null;
//            if(globalVendor == null) {
//                globalVendor = rs.getString("vendor");
//                po = PurchaseOrderDAO.getInstance().create();
//                oList = new LinkedList();
//                Vendor vend = VendorDAO.getInstance().read(rs.getString("vendor"), conn);
//                po.setVendor(vend);
//                
//            }
//            if(globalVendor == rs.getString("vendor")) {
//                VendorItem v = VendorItemDAO.getInstance().create();
//                v.setAlreadyInDB(rs.getBoolean("AlreadyinDB"));
//                v.setConceptualprod(ConceptualProductDAO.getInstance().read(rs.getString("ConceptualProdID"), conn));
//                v.setCost(rs.getDouble("Cost"));
//                v.setDirty(rs.getBoolean("dirty"));
//                v.setVendor(VendorDAO.getInstance().read(rs.getString("vendorID"), conn));        
//                
//                OrderLine ol = OrderLineDAO.getInstance().create();
//                ol.setAlreadyInDB(false);
//                ol.setDirty(true);
//                ol.setOrder(po);
//                ol.setQuantity(StoreProductDAO.getInstance().read(rs.getString("ConceptualProductID")).getQtytoorder());
//                ol.setVendoritem(v);
//                
//                oList.add(ol);
//            }else{
//                
//                po.setAlreadyInDB(false);
//                po.setDirty(true);
//                po.setOrderlines(oList);
//                
//                PurchaseOrderDAO.getInstance().save(po, conn); 
//                
//                po = PurchaseOrderDAO.getInstance().create();
//                oList = new LinkedList();
//            }
//        }
//        
//        
//    }
//    
//}
